package com.glory.huaweipda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import com.glory.huaweipda.R
import com.glory.huaweipda.base.Constant
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.viewmodel.BatchTrackOutModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_system_set.*

class SystemSetActivity : BaseActivity<BatchTrackOutModel>(){

    var type: String = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    type = "oo"
                    val intent = Intent()
                    intent.putExtra("showPlugin", "errorLog")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this@SystemSetActivity, clazz)
                    startActivity(intent)
                }
            }
        }
    }

    var REQUEST_CODE = 1
    override fun layoutId(): Int {
        return R.layout.activity_system_set
    }

    override fun initView(savedInstanceState: Bundle?) {

        val instance = MyApp.instance
        instance!!.handler = handler
        tvServerText.setText(Constant.mainUrl)
        tvVersion.text =
            application.getPackageManager().getPackageInfo(application.getPackageName(), 0).versionName.toString()
        val area = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "area",
            applicationContext)
        val spConnectTimeout = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "connectTimeout",
            applicationContext)
        tvRegionText.text = area
        var i:Int
        if (spConnectTimeout.equals("")){
            i = Constant.connectTimeout / 1000
        }else{
            i = spConnectTimeout.toInt() / 1000
        }
        tvFileText.text = Constant.REQUEST_URL_DEBUG
        connectTimeout.text = i.toString()+"秒"

        rlServerAddress.setOnClickListener{
            val intent = Intent()
            intent.putExtra("showPlugin", "server_address")
            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
            intent.setClass(this, clazz)
            startActivityForResult(intent, REQUEST_CODE)
        }

        rlFileAddress.setOnClickListener{
            val intent = Intent()
            intent.putExtra("showPlugin", "file_address")
            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
            intent.setClass(this, clazz)
            startActivityForResult(intent, REQUEST_CODE)
        }

        rlConnectTimeout.setOnClickListener{
            val intent = Intent()
            intent.putExtra("showPlugin", "connectTimeout")
            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
            intent.setClass(this, clazz)
            startActivityForResult(intent, REQUEST_CODE)
        }

        rlRegionSelection.setOnClickListener{
            viewModel.defaultGetAreaList()
            viewModel.defUI.callObserve.observe(this){
                if (!type.equals("oo")) {
                    val intent = Intent()
                    intent.putExtra("showPlugin", "region_selection")
                    intent.putStringArrayListExtra("areaList", viewModel.areaArrayList)
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this, clazz)
                    startActivityForResult(intent, REQUEST_CODE)
                }
            }
        }

//        btnSubmit.setOnClickListener{
//            val intent = Intent()
//            intent.putExtra("showPlugin", "system_set_bot")
//            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
//            intent.setClass(this, clazz)
//            startActivity(intent)
//        }

        ivBack.setOnClickListener{
            finish()
        }

    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode==REQUEST_CODE) {
            when(resultCode){
                1 ->{
                    val extras = data!!.getExtras()
                    val string = extras!!.getString("server_address")
                    tvServerText.setText(string)
                    FreeApi_SharePreferencesUtils.setSharePre("SystemSet", "sercerUrl", string, applicationContext)
                    Constant.mainUrl = string!!
                    viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)
                }
                2 ->{
                    val extras = data!!.getExtras()
                    val string = extras!!.getString("file_address")
                    tvFileText.setText(string)
                    FreeApi_SharePreferencesUtils.setSharePre("SystemSet", "REQUEST_URL_DEBUG", string, applicationContext)
                    Constant.REQUEST_URL_DEBUG = string!!
                    viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)

                }
                3 ->{
                    val extras = data!!.getExtras()
                    val string = extras!!.getString("region_selection")
                    tvRegionText.setText(string)
                    FreeApi_SharePreferencesUtils.setSharePre("SystemSet","area",string,applicationContext)
                    viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)
                }
                4 ->{
                    val extras = data!!.getExtras()
                    val string = extras!!.getString("connectTimeout")
                    if (string.equals("")){
                        connectTimeout.setText("")
                    }else{
                        connectTimeout.setText(string+"秒")
                    }

                    Log.e("connectTimeout",string.toString())
                    val s = string + "000"
                    FreeApi_SharePreferencesUtils.setSharePre("SystemSet","connectTimeout",s,applicationContext)

                    Constant.connectTimeout = s.toInt()
                    viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)

                }
            }

        }

    }

    override fun initData() {

    }

}